#pragma once

#include <map>
#include <vector>
#include <functional>



class Application
{
public:
    inline bool Init()
    {
        for (auto item : m_init_funcs)
        {
            for (auto func : item.second)
            {
                try
                {
                    if (!func()) return false;
                }
                catch(...)
                {
                    return false;
                }
            }
        }
        return true;
    }
    inline void Exit()
    {
        for (auto item : m_init_funcs)
        {
            for (auto func : item.second)
            {
                try
                {
                    func();
                }
                catch(...)
                {
                    continue;
                }
            }
        }
    }
    inline void Reload()
    {
        for (auto item : m_init_funcs)
        {
            for (auto func : item.second)
            {
                try
                {
                    func();
                }
                catch(...)
                {
                    return;
                }
            }
        }
    }

public:
    inline void RegInit(std::function<bool()> func, std::int32_t priority = 0)
    {
        m_init_funcs[priority].emplace_back(func);
    }
    inline void RegExit(std::function<void()> func, std::int32_t priority = 0)
    {
        m_exit_funcs[priority].emplace_back(func);
    }
    inline void RegReload(std::function<void()> func, std::int32_t priority = 0)
    {
        m_reload_funcs[priority].emplace_back(func);
    }

private:
    std::map<std::int32_t, std::vector<std::function<bool()>>> m_init_funcs;
    std::map<std::int32_t, std::vector<std::function<void()>>> m_exit_funcs;
    std::map<std::int32_t, std::vector<std::function<void()>>> m_reload_funcs;
};















